/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.chart.demo;

import java.util.ArrayList;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年8月31日 下午3:23:32
 * @Title <B>todo</B>
 * 
 *        <pre>
 * todo
 * </pre>
 */

public class HorizontalBarChartView extends View {
	private Paint xLinePaint;// 坐标轴 轴线 画笔：
	private Paint hLinePaint;// 坐标轴水平内部 虚线画笔
	private Paint titlePaint;// 绘制文本的画笔
	private Paint paint;// 矩形画笔 柱状图的样式信息
	private int[]  progress = { 80, 50, 120, 60, 150};// 7
	// 条，显示各个柱状的数据
	private int[] aniProgress;// 实现动画的值
	//private final int TRUE = 1;// 在柱状图上显示数字
	// private int[] text;// 设置点击事件，显示哪一条柱状的信息
	// private Bitmap bitmap;
	// 坐标轴左侧的数标
	private String[] ySteps=new String[]{"远程分析","检查项","复诊","除诊","随访"};
	// 坐标轴底部的星期数
	// private String[] xWeeks;
	private int flag;// 是否使用动画

	private HistogramAnimation ani;

	/**
	 * <pre>
	 * Constructor
	 * </pre>
	 * @param context
	 */
	public HorizontalBarChartView(Context context) {
		this(context, null, 0);
	}

	/**
	 * <pre>
	 * Constructor
	 * </pre>
	 * 
	 * @param context
	 * @param attrs
	 */
	public HorizontalBarChartView(Context context, AttributeSet attrs) {
		this(context, null, 0);
		//
	}

	/**
	 * <pre>
	 * Constructor
	 * </pre>
	 * 
	 * @param context
	 * @param attrs
	 * @param defStyleAttr
	 */
	public HorizontalBarChartView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		//
		init();
	}

	private void init() {
		aniProgress = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		ani = new HistogramAnimation();
		ani.setDuration(2000);

		xLinePaint = new Paint();
		hLinePaint = new Paint();
		titlePaint = new Paint();
		paint = new Paint();

		// 给画笔设置颜色
		xLinePaint.setColor(Color.DKGRAY);
		hLinePaint.setColor(Color.LTGRAY);
		titlePaint.setColor(Color.BLACK);

		// 加载画图
		// bitmap = BitmapFactory.decodeResource(getResources(),
		// R.drawable.column);
	}

	public void start(int flag) {
		this.flag = flag;
		this.startAnimation(ani);
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		int width = getWidth();
		int height = 300;//getHeight() - dp2px(200);
		// 绘制底部的线条
		//canvas.drawLine(dp2px(30), height + dp2px(3), width - dp2px(30), height + dp2px(3), xLinePaint);
		int leftHeight = height - dp2px(5);// 左侧外周的 需要划分的高度：
		int hPerHeight = leftHeight / ySteps.length;// 分成四部分
		hLinePaint.setTextAlign(Align.CENTER);
		// 绘制 Y 周坐标
		titlePaint.setTextAlign(Align.RIGHT);
		titlePaint.setTextSize(sp2px(12));
		titlePaint.setAntiAlias(true);
		titlePaint.setStyle(Paint.Style.FILL);
		
		// 设置左部的数字
		for (int i = 0; i < ySteps.length; i++) {
			canvas.drawText(ySteps[i], dp2px(50), dp2px(20) + i * hPerHeight, titlePaint);
		}
		//左侧的竖线
		canvas.drawLine(dp2px(60), 0,dp2px(60),(float)height,xLinePaint);
		
		float max=progress[0];
		for (int i:progress) {
			if(i>max)max=i;
		}
		if(max>100 && max<=1000){
			max=1000;
		}
		
		//float xSteps=width/max;

		// 绘制矩形
		if (progress != null && progress.length > 0) {
			for (int i = 0; i < progress.length; i++) {// 循环遍历将7条柱状图形画出来
				int value = progress[i];
				paint.setAntiAlias(true);// 抗锯齿效果
				paint.setStyle(Paint.Style.FILL);
				paint.setTextSize(sp2px(15));// 字体大小
				paint.setColor(Color.parseColor("#6DCAEC"));// 字体颜色
				Rect rect = new Rect();// 柱状图的形状
				
				rect.left =dp2px(60);
				rect.top =30;  //hPerHeight;//20;//dp2px(30) + hPerHeight * (i + 1);
				
				int rh =500;//value;//(int) (value / width);
				rect.right = rh;//;
				rect.bottom =70; //hPerHeight * (i + 1);
				System.out.println(rect.toString());
				canvas.drawRect(rect, paint);
				//canvas.drawRect(new Rect(15, 10, 500, 70), paint);
				//canvas.drawBitmap(bitmap, null, rect, paint);
				// 是否显示柱状图上方的数字
//				if (this.text[i] == TRUE) {
//					canvas.drawText(value + "", dp2px(15) + step * (i + 1) - dp2px(15), rh + dp2px(5), paint);
//				}
			}
		}

	}

	private int dp2px(int value) {
		float v = getContext().getResources().getDisplayMetrics().density;
		return (int) (v * value + 0.5f);
	}

	private int sp2px(int value) {
		float v = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (v * value + 0.5f);
	}



	/**
	 * 集成animation的一个动画类
	 * 
	 * @author 李垭超
	 */
	private class HistogramAnimation extends Animation {
		protected void applyTransformation(float interpolatedTime, Transformation t) {
			super.applyTransformation(interpolatedTime, t);
			if (interpolatedTime < 1.0f && flag == 2) {
				for (int i = 0; i < aniProgress.length; i++) {
					aniProgress[i] = (int) (progress[i] * interpolatedTime);
				}
			} else {
				for (int i = 0; i < aniProgress.length; i++) {
					aniProgress[i] = progress[i];
				}
			}
			invalidate();
		}
	}

}
